#! /usr/bin/python

import os
import subprocess

clang_format = os.getenv('CLANG_FORMAT')
if not clang_format:
    print('Please set CLANG_FORMAT enviroment to the clang-format full path.')
    sys.exit(-1)

def paths(path):
    path_collection = []
    for dirpath, dirnames, filenames in os.walk(path):
        for file in filenames:
            fullpath = os.path.join(dirpath, file)
            path_collection.append(fullpath)
    return path_collection
    
for p in paths('./'):
    if p.endswith('.c') or \
        p.endswith('cpp') or \
        p.endswith('.cc') or \
        p.endswith('.h') or \
        p.endswith('.hpp') or \
        p.endswith('.m') or \
        p.endswith('.mm'):
            print('Formating "%s" ...' % (p))
            subprocess.Popen([clang_format, '-i', '-style=Google', p]).wait()
